﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using TimKen.DataEntity;
namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类Address。
    /// </summary>
    public class AddressDAO
    {
        public AddressDAO()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string AddressCode, string AdresType, string CardCode)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@AddressCode", SqlDbType.NVarChar,50),
					new SqlParameter("@AdresType", SqlDbType.VarChar,50),
					new SqlParameter("@CardCode", SqlDbType.NVarChar,50)};
            parameters[0].Value = AddressCode;
            parameters[1].Value = AdresType;
            parameters[2].Value = CardCode;

            int result = DbHelperSQL.RunProcedure("UP_Address_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.Address model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@AddressCode", SqlDbType.NVarChar,50),
					new SqlParameter("@CardCode", SqlDbType.NVarChar,15),
					new SqlParameter("@Street", SqlDbType.NVarChar,100),
					new SqlParameter("@Block", SqlDbType.NVarChar,100),
					new SqlParameter("@ZipCode", SqlDbType.NVarChar,20),
					new SqlParameter("@City", SqlDbType.NVarChar,100),
					new SqlParameter("@County", SqlDbType.NVarChar,100),
					new SqlParameter("@Country", SqlDbType.NVarChar,3),
					new SqlParameter("@State", SqlDbType.NVarChar,3),
					new SqlParameter("@AdresType", SqlDbType.VarChar,1),
					new SqlParameter("@Building", SqlDbType.Text),
                    new SqlParameter("@LanguageID", SqlDbType.Int), 
                    new SqlParameter("@ContactPerson", SqlDbType.NVarChar,50),
                    new SqlParameter("@Phone", SqlDbType.NVarChar,50),
                    new SqlParameter("@GNum", SqlDbType.NVarChar,50)
                                        };
            parameters[0].Value = model.AddressCode;
            parameters[1].Value = model.CardCode;
            parameters[2].Value = model.Street;
            parameters[3].Value = model.Block;
            parameters[4].Value = model.ZipCode;
            parameters[5].Value = model.City;
            parameters[6].Value = model.County;
            parameters[7].Value = model.Country;
            parameters[8].Value = model.State;
            parameters[9].Value = model.AdresType;
            parameters[10].Value = model.Building;
            parameters[11].Value = model.LanguageID;
            parameters[12].Value = model.AdresType;
            parameters[13].Value = model.Building;
            parameters[14].Value = model.LanguageID;

            DbHelperSQL.RunProcedure("UP_Address_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.Address model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@AddressCode", SqlDbType.NVarChar,50),
					new SqlParameter("@CardCode", SqlDbType.NVarChar,15),
					new SqlParameter("@Street", SqlDbType.NVarChar,100),
					new SqlParameter("@Block", SqlDbType.NVarChar,100),
					new SqlParameter("@ZipCode", SqlDbType.NVarChar,20),
					new SqlParameter("@City", SqlDbType.NVarChar,100),
					new SqlParameter("@County", SqlDbType.NVarChar,100),
					new SqlParameter("@Country", SqlDbType.NVarChar,3),
					new SqlParameter("@State", SqlDbType.NVarChar,3),
					new SqlParameter("@AdresType", SqlDbType.VarChar,1),
					new SqlParameter("@Building", SqlDbType.Text)};
            parameters[0].Value = model.AddressCode;
            parameters[1].Value = model.CardCode;
            parameters[2].Value = model.Street;
            parameters[3].Value = model.Block;
            parameters[4].Value = model.ZipCode;
            parameters[5].Value = model.City;
            parameters[6].Value = model.County;
            parameters[7].Value = model.Country;
            parameters[8].Value = model.State;
            parameters[9].Value = model.AdresType;
            parameters[10].Value = model.Building;

            DbHelperSQL.RunProcedure("UP_Address_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string AddressCode, string AdresType, string CardCode)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@AddressCode", SqlDbType.NVarChar,50),
					new SqlParameter("@AdresType", SqlDbType.VarChar,50),
					new SqlParameter("@CardCode", SqlDbType.NVarChar,50)};
            parameters[0].Value = AddressCode;
            parameters[1].Value = AdresType;
            parameters[2].Value = CardCode;

            DbHelperSQL.RunProcedure("UP_Address_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.Address GetModel(string AddressCode, string AdresType, string CardCode)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@AddressCode", SqlDbType.NVarChar,50),
					new SqlParameter("@AdresType", SqlDbType.VarChar,50),
					new SqlParameter("@CardCode", SqlDbType.NVarChar,50)};
            parameters[0].Value = AddressCode;
            parameters[1].Value = AdresType;
            parameters[2].Value = CardCode;

            TimKen.DataEntity.Address model = new TimKen.DataEntity.Address();
            DataSet ds = DbHelperSQL.RunProcedure("UP_Address_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.AddressCode = ds.Tables[0].Rows[0]["AddressCode"].ToString();
                model.AdresType = ds.Tables[0].Rows[0]["AdresType"].ToString();
                model.Building = ds.Tables[0].Rows[0]["Building"].ToString();
                model.CardCode = ds.Tables[0].Rows[0]["CardCode"].ToString();
                model.Street = ds.Tables[0].Rows[0]["Street"].ToString();
                model.Block = ds.Tables[0].Rows[0]["Block"].ToString();
                model.ZipCode = ds.Tables[0].Rows[0]["ZipCode"].ToString();
                model.City = ds.Tables[0].Rows[0]["City"].ToString();
                model.County = ds.Tables[0].Rows[0]["County"].ToString();
                model.Country = ds.Tables[0].Rows[0]["Country"].ToString();
                model.State = ds.Tables[0].Rows[0]["State"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AddressCode,CardCode,Street,Block,ZipCode,City,County,Country,State,AdresType,Building,LanguageID ");
            strSql.Append(" FROM Address ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public List<TimKen.DataEntity.Address> GetAddressList(string strWhere) 
        {
            DataSet ds = GetList(strWhere);
            List<TimKen.DataEntity.Address> list = new List<TimKen.DataEntity.Address>();
            foreach (DataRow dr in ds.Tables[0].Rows) 
            {
                   TimKen.DataEntity.Address model = new TimKen.DataEntity.Address();
                   model.AddressCode = dr["AddressCode"].ToString();
                   model.CardCode = dr["CardCode"].ToString();
                   model.Street = dr["Street"].ToString();
                   model.Block = dr["Block"].ToString();
                   model.ZipCode = dr["ZipCode"].ToString();
                   model.City = dr["City"].ToString();
                   model.County = dr["County"].ToString();
                   model.Country = dr["Country"].ToString();
                   model.State = dr["State"].ToString();
                   model.AdresType = dr["AdresType"].ToString();
                   model.Building = dr["Building"].ToString();
                   model.LanguageID = int.Parse(dr["LanguageID"].ToString());
                  list.Add(model);
            }
            return list;
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Address";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法


        #region 常用地址
        public bool InserAddress(CommonAddress ad)
        {
           string sql="insert into CommonAddress values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')";
          int i= DbHelperSQL.ExecuteSql(string .Format(sql,ad.Consignee,ad.PostalCode,ad.Address,ad.Phone,ad.Telephone,ad.Email,ad.Remark,ad.UserName
                  ));
          if (i > 0)
          {
              return true;
          }
          else {
              return false;
          }
        
        }

        public bool UpdateAddress(CommonAddress ad)
        {
            string sql = "update CommonAddress set Consignee='{0}',PostalCode='{1}',Address='{2}',Phone='{3}',Telephone='{4}',Email='{5}',Remark='{6}' where id='{7}'";
            int i = DbHelperSQL.ExecuteSql(string.Format(sql, ad.Consignee, ad.PostalCode, ad.Address, ad.Phone, ad.Telephone, ad.Email, ad.Remark, ad.ID
                 ));
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteAddress(string id)
        {
            string sql = "delete from CommonAddress where id='{0}'";
            int i = DbHelperSQL.ExecuteSql(string.Format(sql, id));
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public string isExitsAddress(string address,string name)
        {
            string sql = "select id from CommonAddress where Address='{0}' and Consignee='{1}'";
           object obj= DbHelperSQL.GetData(string.Format(sql, address,name));
           if (obj != null && obj.ToString() != "")
           {
               return obj.ToString();
           }
           else {
               return "";
           }

        }
        public DataTable GetAddressByUser(string user)
        {
            string sql = "select * from CommonAddress where UserName='{0}'";
           DataSet dt= DbHelperSQL.GetDataSet(string.Format(sql, user));
           if (dt.Tables.Count > 0)
           {
               return dt.Tables[0];
           }
           else {
               return null;
           }
        }

        public DataTable GetAddressByid(string id)
        {
            string sql = "select * from CommonAddress where id='{0}'";
            DataSet dt = DbHelperSQL.GetDataSet(string.Format(sql, id));
            if (dt.Tables.Count > 0)
            {
                return dt.Tables[0];
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}
